<script>
	export default {
		onLaunch: function() {
			// console.log('App Launch')
		},
		onShow: function() {
			// console.log('App Show')
			this.init()
		},
		onHide: function() {
			// console.log('App Hide')
		},
		methods:{
			login() {
				var data;
				return new Promise((resolve, reject) => {
					uni.login({
						success(res) {
							resolve(res)
						},
						fail(err) {
							reject(err);
						}
					})
				})
			},
			async init() {
				// this.appLoginWx()
			    //调用微信登陆
			    let res = await this.login()
			    if (res.code && ['login:ok'].includes(res.errMsg)) {
			  //       //后端通过code调用微信API返回openid/unionid/session_key参数， 存储起来,前端无法直接调用微信相关API
					// console.log(res.code,'rescode');
			        let openIdRes = await this.getWechatOpenIdByCode(res.code)
					console.log("openIDRes:",openIdRes);
					uni.setStorageSync('openId',openIdRes.data.data.openId)
					uni.setStorageSync('token',openIdRes.data.token)
			    } else {
			        // 异常处理，再次发起请求或者抛出异常
			    }
			},
			getWechatOpenIdByCode(code){
				return new Promise((resolve,reject)=>{
					this.$myRequest({
						url: '/user/login',
						method: 'PUT',
						data:{
							code:code
						},
					}).then(res=>{
						resolve(res)
					}).catch(err=>{
						console.log(err);
					})
				})	
			},
		}
	}
</script>

<style>
	/*每个页面公共css */
</style>
